<template>
 <el-dialog
  :title="title"
  :visible="dialogVisible"
  @close='closeDialog'
  @open='getnewlist'
  width="420px">
    <el-form  label-width="80px" ref="refForm" :model="w" :rules="rules">
      <el-form-item label="所属学科" prop="subjectID" v-if="subjectID===''">
    <el-select placeholder="请选择" style="width:300px" v-model="w.subjectID">
      <el-option v-for="(item,index) in newList" :key="index" :value="item.id" :label="item.subjectName"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="目录名称" prop='directoryName'>
    <el-input style="width:300px" v-model.trim="w.directoryName" placeholder="请输入目录名称"></el-input>
  </el-form-item>
   </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary" @click="sumbitAdd">确 定</el-button>
  </span>
</el-dialog>
</template>

<script>
import { list } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  name: 'DirectorysAdd',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      required: true
    },
    subjectID: {
      type: String,
      default: ''
    }
  },
  created () {
    this.getnewlist()
  },
  data () {
    return {
      w: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: ['blur', 'change'] }
        ],
        subjectID: [
          { required: true, message: '所属学科不能为空', trigger: ['blur', 'change'] }
        ]
      },
      newList: [], // 去重后的目录列表
      title: '新增目录'
    }
  },
  methods: {
    // 关闭对话框
    closeDialog () {
      this.$emit('update:dialogVisible', false)
      this.$refs.refForm.resetFields()
      this.w = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 获取学科列表
    async getnewlist () {
      // this.list.forEach(item => {
      //   if (!this.newList.find(value => value.subjectName === item.subjectName)) {
      //     this.newList.push(item)
      //   }
      // })
      const { data } = await list()
      console.log(data)
      this.newList = data.items
    },
    // 新增或修改目录
    sumbitAdd () {
      this.$refs.refForm.validate(async boo => {
        if (boo) {
          // 判断是跳转修改还是默认修改
          if (this.w.id) {
            if (this.subjectID) {
              this.w.subjectID = +this.subjectID
              await update(this.w)
              this.$emit('agagintwo')
              this.$message.success('修改成功')
            } else {
              await update(this.w)
              this.$emit('again')
              this.$message.success('修改成功')
            }
          } else {
            // 判断是跳转添加还是默认添加
            if (this.subjectID) {
              this.w.subjectID = +this.subjectID
              await add(this.w)
              this.$emit('agagintwo')
              this.$message.success('添加成功')
            } else {
              // 1.发送新增请求s
              await add(this.w)
              this.$emit('again')
              this.$message.success('添加成功')
            }
          }
          // 3.关闭对话框
          this.closeDialog()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
